package com.zhou.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.yygh.common.result.Result;
import com.zhou.yygh.common.utills.MD5;
import com.zhou.yygh.hosp.service.HospitalSetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import zhou.hospital.model.hosp.Hospital;
import zhou.hospital.model.hosp.HospitalSet;
import zhou.hospital.vo.hosp.HospitalSetQueryVo;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@Api(tags = "医院设置管理")

public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

//获取所有医院设置
    @ApiOperation(value = "获取所有用户")
    @GetMapping("/findAll")
    public Result findAllHospitalSet() {
     return   Result.ok(hospitalSetService.list());
    }

//根据id删除医院设置
    @ApiOperation(value = "根据Id删除一个医院设置")
    @DeleteMapping("/delete/{id}")
    public Result deleteHospitalSet(@PathVariable Integer id){
        boolean flag = hospitalSetService.removeById(id);
       if(flag){
          return Result.ok();
       }
          return Result.fail();
    }

    //条件查询医院设置并分页
    @ApiOperation(value = "条件查询医院设置并分页")
    @PostMapping("/findPage/{current}/{limit}")
    public Result findHospSet(@PathVariable long current,
                              @PathVariable long limit,
                              @RequestBody(required = false) HospitalSetQueryVo vo){
        //构建分页查询条件
        Page<HospitalSet> page = new Page<>(current, limit);
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = vo.getHosname();
        String hoscode = vo.getHoscode();
        if(!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname",hosname);
        }
        if(!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode",hoscode);
        }

        //根据条件查询
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, wrapper);
        return Result.ok(hospitalSetPage);

    }


    //添加医院设置
    @ApiOperation(value = "添加医院设置")
    @PostMapping("/saveHospitalSet")
    public  Result saveHospSet(@RequestBody HospitalSet hospitalSet){

        //状态为1使用，0不使用
        hospitalSet.setStatus(1);
        Random random = new Random();
        String s = MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000));
        hospitalSet.setSignKey(s);
        boolean flag = hospitalSetService.save(hospitalSet);
        if(flag){
            return Result.ok();
        }
        return Result.fail();
    }

    //根据id获取医院设置
    @ApiOperation(value = "根据Id获取医院设置")
    @GetMapping("/getHostSet/{id}")
    public Result getHostSetById(@PathVariable int id){

        return Result.ok(hospitalSetService.getById(id));
    }

    //修改医院设置
    @ApiOperation(value = "修改医院设置")
    @PostMapping("/updateHostSet")
    public Result updateHostSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag){
            return Result.ok();
        }
        return Result.fail();
    }

   //根据Id批量修改医院设置
    @DeleteMapping("/batchRemove")
    public Result batchRemoveHostSet(@RequestBody List<Long> idList){
        boolean flag = hospitalSetService.removeByIds(idList);
        if(flag){
            return Result.ok();
        }
        return Result.fail();
    }

    //医院设置锁定和解锁状态
    @PutMapping("/lockHostSet/{id}/{status}")
    public Result locakHospitalSet(@PathVariable Integer id,
                                   @PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    //发送密钥
}


